package com.rural.bus;

import org.json.JSONException;
import org.json.JSONObject;

import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;
import com.rural.bus.utils.ProgressUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @date 2014/11/14
 * @author majie
 * @description 侧边栏菜单
 */
public class MineActivity extends BaseActivity implements OnClickListener {
	private View tvOrderlist;
	private View settings;
	private View tvShare;
	private View tvCoupon;
	private RelativeLayout myData;
	private TextView username, tv_my_phone, tv_my_account_balance;
	private ImageView userimg;
	
	private boolean isGetBalanceSuccess;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_menu);
		findViews();
	}

	public void findViews() {
		tvOrderlist = findViewById(R.id.tv_orderlist);

		settings = findViewById(R.id.tvMySettings);
		tvShare = findViewById(R.id.tvShare);
		tvCoupon = findViewById(R.id.tvCoupon);

		myData = (RelativeLayout) findViewById(R.id.my_data);

		username = (TextView) findViewById(R.id.user_name);
		userimg = (ImageView) findViewById(R.id.avatar_pic);
		userimg.setOnClickListener(this);
		settings.setOnClickListener(this);
		myData.setOnClickListener(this);
		tvShare.setOnClickListener(this);
		tvCoupon.setOnClickListener(this);
		tvOrderlist.setOnClickListener(this);

		tv_my_account_balance = (TextView) findViewById(R.id.tv_my_account_balance);
		tv_my_phone = (TextView) findViewById(R.id.tv_my_phone);

		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		mBtMiddle.setText("个人中心");

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.translate_in_right, R.anim.translate_out_left);
			}
		});

		getUserBalance();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mCurrentUser != null) {
			username.setText(mCurrentUser.name);
			tv_my_phone.setText(mCurrentUser.phone);
		} else {
			username.setText("未登录");
		}
		if(!isGetBalanceSuccess){
			getUserBalance();
		}
	}

	private void getUserBalance() {
		if(mCurrentUser == null){
			return;
		}
		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("haveId", mCurrentUser.userId);
		http.addBodyParm("type", "0");
		http.send(Url.money_url, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				isGetBalanceSuccess = true;
				Log.i("MyAccountActivity", object.toString());
				String result = object.optString("result", "x");
				tv_my_account_balance.setText(result + " 元");

			}

			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast(error);
			}
		});
	}
	
	
	@Override
	public void onClick(View v) {
		// Fragment newContent = null;
		// String title = null;
		switch (v.getId()) {
		case R.id.tvMySettings: // 设置
			startActivity(new Intent(this, SettingActivity.class));
			break;

		case R.id.tv_orderlist:
			startActivity(new Intent(this, OrderListActivity.class));
			break;
		case R.id.tvCoupon:
			// getActivity().startActivity(new Intent(getActivity(),
			// CouponActivity.class));
			break;
		case R.id.my_data:
		case R.id.avatar_pic:
			if (mCurrentUser == null) {
				startActivity(new Intent(this, LoginActivity.class));
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 切换fragment
	 * 
	 * @param fragment
	 */
	// private void switchFragment(Fragment fragment, String title) {
	// if (getActivity() == null) {
	// return;
	// }
	// // if (getActivity() instanceof NewMainActivity) {
	// // NewMainActivity fca = (NewMainActivity) getActivity();
	// // fca.switchContent(fragment, title);
	// // }
	// }

	// private void downloadUserPic(){
	// String
	// dir=FileUtils.getSDCardRoot()+getActivity().getPackageName()+File.separator;
	// File dirFile=new File(dir);
	// if(!dirFile.exists()){
	// dirFile.mkdirs();
	// }
	// MyUser myUser=BmobUser.getCurrentUser(getActivity(), MyUser.class);
	// if(myUser==null){
	// return;
	// }
	// }

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void setListeners() {

	}

}
